<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      
      <!--
      <el-form-item label="教师姓名" prop="teacherId">
        <el-input
          v-model="queryParams.teacherId"
          placeholder="请输入教师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="院系名称" prop="courseclassId">
        <el-input
          v-model="queryParams.courseclassId"
          placeholder="请输入院系名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      -->
      <el-form-item label="教师姓名" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入教师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动" prop="evalutionActivityId" >
        <el-select v-model="queryParams.evalutionActivityId" placeholder="请选择活动" clearable>
          <el-option
            v-for="activity in activityList"
            :key="activity.evalutionActivityId"
            :label="activity.title"
            :value="activity.evalutionActivityId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="院系名称" prop="departmentName" v-show="false">
        <el-input
          v-model="queryParams.departmentName"
          placeholder="请输入院系名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属部门" >
        <treeselect v-model="deptId" :options="deptOptions" :show-count="true" style="width: 180px" placeholder="请选择归属部门" @select="handleTableNodeClick"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-divider><i class="el-icon-s-grid"></i>课程评价结果统计</el-divider>

    <el-table v-loading="loading" :data="statisticsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50"> </el-table-column>
      <el-table-column label="被评教师" align="center" prop="realName" />
      <el-table-column label="工号" align="center" prop="teacherId" />
      <el-table-column label="院系" align="center" prop="deptName" />
      <el-table-column label="课程ID" align="center" prop="courseClassNo" />
      <el-table-column label="课程名" align="center" prop="courseName" />
      <el-table-column label="得分" align="center" prop="score" />
      <el-table-column label="名次" align="center" prop="rankNo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-checked"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pingjiao:statistics:edit']"
          >查看得分详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改统计对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评教活动id" prop="evaluationActivityId">
          <el-input v-model="form.evaluationActivityId" placeholder="请输入评教活动id" />
        </el-form-item>
        <el-form-item label="评教问卷id" prop="evaluationPaperId">
          <el-input v-model="form.evaluationPaperId" placeholder="请输入评教问卷id" />
        </el-form-item>
        <el-form-item label="教师id" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入教师id" />
        </el-form-item>
        <el-form-item label="上课班级id" prop="courseclassId">
          <el-input v-model="form.courseclassId" placeholder="请输入上课班级id" />
        </el-form-item>
        <el-form-item label="学生id" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学生id" />
        </el-form-item>
        <el-form-item label="是否删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入是否删除" />
        </el-form-item>
        <el-form-item label="结束时间" prop="generateTime">
          <el-date-picker clearable
            v-model="form.generateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总分" prop="sumScore">
          <el-input v-model="form.sumScore" placeholder="请输入总分" />
        </el-form-item>
        <el-form-item label="完成状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入完成状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeacherStatistics, listStatistics, getStatistics, delStatistics, addStatistics, updateStatistics } from "@/api/pingjiao/statistics";
import {listActivity} from "@/api/pingjiao/activity";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Statistics",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 统计表格数据
      statisticsList: [
        /*
        {
         id:0,//序号
         teacher:0,//被评教师
         jobNO:0,//工号
         department:0,//院系
         classId:0,//课程号
         className:0,//课程名
         score:0,//得分
         NO:0,//名次
         operate:"查看参评人详细得分",//查看参评人详细得分
        }
         */
      ],
      // 评教活动管理表格数据
      activityList: [],
      // 部门树选项
      deptOptions: [],
      //部门ID
      deptId: undefined,
      // 部门名称
      deptName: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        evaluationActivityId: null,
        evaluationPaperId: null,
        teacherId: null,
        courseclassId: null,
        studentId: null,
        status: null,
        generateTime: null,
        sumScore: null,
        state: null,
        teacherName: null,
        departmentName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  components: {Treeselect},
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getDeptTree();
    this.getList();
    this.getActivityList();
  },
  methods: {
    /** 查询统计列表 */
    getList() {
      this.loading = true;
      listTeacherStatistics(this.queryParams).then(response => {
         this.statisticsList = response.rows;
         this.total = response.total;
         this.loading = false;
       });
        this.loading = false;
    },
    /** 查询评教活动管理列表 */
    getActivityList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.activityList = response.rows;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    // 插入或修改提升框节点单击事件
    handleTableNodeClick(data) {
      console.log(data);
      this.queryParams.departmentName = data.label;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.deptId =  undefined;
      this.form = {
        evaluationId: null,
        evaluationActivityId: null,
        evaluationPaperId: null,
        teacherId: null,
        courseclassId: null,
        studentId: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        generateTime: null,
        sumScore: null,
        state: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.evaluationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const evaluationId = row.evaluationId || this.ids
      getStatistics(evaluationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.evaluationId != null) {
            updateStatistics(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStatistics(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const evaluationIds = row.evaluationId || this.ids;
      this.$modal.confirm('是否确认删除统计编号为"' + evaluationIds + '"的数据项？').then(function() {
        return delStatistics(evaluationIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pingjiao/statistics/export', {
        ...this.queryParams
      }, `statistics_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
